package com.jn.okhttp.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.okhttp.R;
import com.jn.okhttp.adapter.FoodAdapter;
import com.jn.okhttp.base.BaseActivity;
import com.jn.okhttp.contract.FoodContract;
import com.jn.okhttp.entity.FoodEntity;
import com.jn.okhttp.presenter.FoodPresenter;

import java.util.ArrayList;
import java.util.List;

public class FoodActivity extends BaseActivity implements FoodContract.IFoodView, View.OnClickListener {

    private FoodContract.IFoodPresenter mPresenter;
    private Button mButton;
    private EditText mEdit;
    private RecyclerView mRecyclerView;
    private FoodAdapter mAdapter;
    private List<FoodEntity.DataItem> mList;
    private ProgressBar mProgressBar; // 新增进度条

    @Override
    public int getLayoutId() {
        return R.layout.activity_food;
    }

    @Override
    public void init() {
        mPresenter = new FoodPresenter(this, this);
        mButton = findViewById(R.id.button);
        mEdit = findViewById(R.id.edit);
        mRecyclerView = findViewById(R.id.recyclerView);
        mProgressBar = findViewById(R.id.progressBar); // 初始化进度条

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new FoodAdapter(this, mList); // 传入上下文
        mRecyclerView.setAdapter(mAdapter);
        mButton.setOnClickListener(this);
    }

    @Override
    public void showData(FoodEntity data) {
        mProgressBar.setVisibility(View.GONE); // 隐藏进度条
        if (data != null && data.getData() != null) {
            mList.clear(); // 清空旧数据
            mList.addAll(data.getData()); // 添加新数据
            mAdapter.updateData(mList);
        }
    }

    @Override
    public void onHttpLoading() {
        mProgressBar.setVisibility(View.VISIBLE); // 显示进度条
    }

    @Override
    public void onHttpFail(String... msg) {
        mProgressBar.setVisibility(View.GONE); // 隐藏进度条
        // 显示错误消息
        String errorMsg = (msg != null && msg.length > 0) ? msg[0] : "请求失败，请重试";
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHttpTimeout() {
        mProgressBar.setVisibility(View.GONE); // 隐藏进度条
        Toast.makeText(this, "请求超时，请稍后再试", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHttpTokenExpiration() {
        mProgressBar.setVisibility(View.GONE); // 隐藏进度条
        Toast.makeText(this, "登录状态已过期，请重新登录", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String keyWord = mEdit.getText().toString();
            if (!TextUtils.isEmpty(keyWord)) {
                mPresenter.getData(keyWord);
            } else {
                Toast.makeText(this, "请输入关键字", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
